﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 自动电子凸轮运动 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        public 自动电子凸轮运动()
        {
            InitializeComponent();
            this.DoubleBuffered = true;
            adapt.SetForm(this);
            checkBox_op.Enabled = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect) {
                int mainAxis=0, fAxis=0;
                float mainPos=0, fPos=0;
                uint ioState = 0;
                mainAxis = (int)numericUpDown_mainAxis.Value;
                fAxis = (int)numericUpDown_flowAxis.Value;

                PMC_GetDpos(_handle, mainAxis, ref mainPos);
                PMC_GetDpos(_handle, fAxis, ref fPos);
                PMC_GetOp(_handle, 0, ref ioState);

                label_mainPos.Text= mainPos.ToString();
                label_faxisPos.Text= fPos.ToString();

                checkBox_op.Checked =Convert.ToBoolean(ioState);
            }
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            if (isConnect) {
                PMC_Rapidstop(_handle, 3);
            }
        }

        private async void Button3_Click(object sender, EventArgs e)
        {
           
            if (isConnect) {
                PMC_Rapidstop(_handle, 3);
                Button3.Enabled = false;
                int mainAxis = 0, fAxis = 0;
                mainAxis = (int)numericUpDown_mainAxis.Value;
                fAxis = (int)numericUpDown_flowAxis.Value;
                PMC_SetMerge(_handle, mainAxis, 1);
                PMC_SetMerge(_handle, fAxis, 1);
                PMC_SetSpeed(_handle, mainAxis,(float) numericUpDown_mainSpd.Value);
                await Task.Run(() =>
                {
                //判断缓冲剩余量:如果缓冲小于20等待一下
                int[] axisList = { fAxis, mainAxis };
                    while (Data.ReMainBuffer(axisList) <= 20)
                    {
                        Thread.Sleep(200);

                        if (RadioButton_pos.Checked)
                        {
                            PMC_Movelink(_handle, fAxis, (float)numericUpDown_pos_11.Value, (float)numericUpDown_pos_12.Value, (float)numericUpDown_pos_13.Value, (float)numericUpDown_pos_14.Value, mainAxis, 0, 0);//速度同步
                            PMC_MoveOp(_handle, fAxis, 0, 1);      //开剪
                            PMC_Movelink(_handle, fAxis, (float)numericUpDown_pos_21.Value, (float)numericUpDown_pos_22.Value, (float)numericUpDown_pos_23.Value, (float)numericUpDown_pos_24.Value, mainAxis, 0, 0); //速度跟随
                            PMC_MoveOp(_handle, fAxis, 0, 0);      //关剪
                            PMC_Movelink(_handle, fAxis, (float)numericUpDown_pos_31.Value, (float)numericUpDown_pos_32.Value, (float)numericUpDown_pos_33.Value, (float)numericUpDown_pos_34.Value, mainAxis, 0, 0);   //减速
                        }
                        else
                        {
                            PMC_Moveslink(_handle, fAxis, (float)numericUpDown_spd_11.Value, (float)numericUpDown_spd_12.Value, (float)numericUpDown_spd_13.Value, (float)numericUpDown_spd_14.Value, mainAxis, 0, 0);//速度同步
                            PMC_MoveOp(_handle, fAxis, 0, 1);      //开剪
                            PMC_Moveslink(_handle, fAxis, (float)numericUpDown_spd_21.Value, (float)numericUpDown_spd_22.Value, (float)numericUpDown_spd_23.Value, (float)numericUpDown_spd_24.Value, mainAxis, 0, 0); //速度跟随
                            PMC_MoveOp(_handle, fAxis, 0, 0);      //关剪
                            PMC_Moveslink(_handle, fAxis, (float)numericUpDown_spd_31.Value, (float)numericUpDown_spd_32.Value, (float)numericUpDown_spd_33.Value, (float)numericUpDown_spd_34.Value, mainAxis, 0, 0);

                        }
                        PMC_Single_Move(_handle, mainAxis, (float)numericUpDown_mainDis.Value);

                        int idle = 0; int ret = 0;
                        int mainAxis1 = (int)numericUpDown_mainAxis.Value;
                        while (idle == 0 && ret == 0)
                        {
                            ret = PMC_GetIfIdle(_handle, mainAxis1, ref idle);
                        }
                        Button3.Invoke(new Action(() =>
                        {
                            Button3.Enabled = true;
                        }));
                    }
                });
            }
        }
    }
}
